//
//  LJSpecial.m
//
//  Created by   on 2018/9/28
//  Copyright (c) 2018 __MyCompanyName__. All rights reserved.
//

#import "LJSpecial.h"


NSString *const kLJSpecialLianjiaSpecial = @"lianjia_special";
NSString *const kLJSpecialLianjiaSpecialComm = @"lianjia_special_comm";
NSString *const kLJSpecialDeveloperSpecial = @"developer_special";
NSString *const kLJSpecialDeveloperSpecialComm = @"developer_special_comm";


@interface LJSpecial ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation LJSpecial

@synthesize lianjiaSpecial = _lianjiaSpecial;
@synthesize lianjiaSpecialComm = _lianjiaSpecialComm;
@synthesize developerSpecial = _developerSpecial;
@synthesize developerSpecialComm = _developerSpecialComm;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict {
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if (self && [dict isKindOfClass:[NSDictionary class]]) {
            self.lianjiaSpecial = [self objectOrNilForKey:kLJSpecialLianjiaSpecial fromDictionary:dict];
            self.lianjiaSpecialComm = [self objectOrNilForKey:kLJSpecialLianjiaSpecialComm fromDictionary:dict];
            self.developerSpecial = [self objectOrNilForKey:kLJSpecialDeveloperSpecial fromDictionary:dict];
            self.developerSpecialComm = [self objectOrNilForKey:kLJSpecialDeveloperSpecialComm fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation {
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    NSMutableArray *tempArrayForLianjiaSpecial = [NSMutableArray array];
    
    for (NSObject *subArrayObject in self.lianjiaSpecial) {
        if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForLianjiaSpecial addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForLianjiaSpecial addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForLianjiaSpecial] forKey:kLJSpecialLianjiaSpecial];
    [mutableDict setValue:self.lianjiaSpecialComm forKey:kLJSpecialLianjiaSpecialComm];
    NSMutableArray *tempArrayForDeveloperSpecial = [NSMutableArray array];
    
    for (NSObject *subArrayObject in self.developerSpecial) {
        if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForDeveloperSpecial addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForDeveloperSpecial addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForDeveloperSpecial] forKey:kLJSpecialDeveloperSpecial];
    [mutableDict setValue:self.developerSpecialComm forKey:kLJSpecialDeveloperSpecialComm];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description  {
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];

    self.lianjiaSpecial = [aDecoder decodeObjectForKey:kLJSpecialLianjiaSpecial];
    self.lianjiaSpecialComm = [aDecoder decodeObjectForKey:kLJSpecialLianjiaSpecialComm];
    self.developerSpecial = [aDecoder decodeObjectForKey:kLJSpecialDeveloperSpecial];
    self.developerSpecialComm = [aDecoder decodeObjectForKey:kLJSpecialDeveloperSpecialComm];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_lianjiaSpecial forKey:kLJSpecialLianjiaSpecial];
    [aCoder encodeObject:_lianjiaSpecialComm forKey:kLJSpecialLianjiaSpecialComm];
    [aCoder encodeObject:_developerSpecial forKey:kLJSpecialDeveloperSpecial];
    [aCoder encodeObject:_developerSpecialComm forKey:kLJSpecialDeveloperSpecialComm];
}

- (id)copyWithZone:(NSZone *)zone {
    LJSpecial *copy = [[LJSpecial alloc] init];
    
    
    
    if (copy) {

        copy.lianjiaSpecial = [self.lianjiaSpecial copyWithZone:zone];
        copy.lianjiaSpecialComm = [self.lianjiaSpecialComm copyWithZone:zone];
        copy.developerSpecial = [self.developerSpecial copyWithZone:zone];
        copy.developerSpecialComm = [self.developerSpecialComm copyWithZone:zone];
    }
    
    return copy;
}


@end
